package com.houhanzhi.controller;


import com.houhanzhi.controller.request.AccountLoginRequest;
import com.houhanzhi.controller.request.AccountRegisterRequest;
import com.houhanzhi.enums.BizCodeEnum;
import com.houhanzhi.response.JsonData;
import com.houhanzhi.service.AccountService;
import com.houhanzhi.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author houhanzhi
 * @since 2025-01-02
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    /**
     * 文件上传，最大默认1M
     *  可以对文件格式、扩展名称判断
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public JsonData uploadUserImg(@RequestPart("file") MultipartFile file) {
        String result = fileService.uploadUserImg(file);
        return StringUtils.isEmpty(result) ? JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL) : JsonData.buildSuccess(result);
    }


    /**
     * 用户注册
     * @param request
     * @return
     */
    @PostMapping("/register")
    public JsonData register(@RequestBody AccountRegisterRequest request){
        JsonData jsonData = accountService.register(request);
        return jsonData;
    }

    /**
     * 用户登录
     * @param request
     * @return
     */
    @PostMapping("/login")
    public JsonData login(@RequestBody AccountLoginRequest request){
        JsonData jsonData = accountService.login(request);
        return jsonData;
    }
}

